package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysDictType;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysDictTypeService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysDeptController
 * @描述 <br>数字字典类型信息</br>
 * @日期 2019-04-24
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/dict")
@Api(value = "字典类型", description = "数据字典类型信息操作处理")
public class SysDictTypeController extends BaseController {
	private String prefix = "system/dict/type";

	@Autowired
	private SysDictTypeService dictTypeService;

	@RequiresPermissions("system:dict:view")
	@GetMapping()
	public String dictType() {
		return prefix + "/type";
	}

	@ApiOperation(value = "字典列表", notes = "数据字典列表信息")
	@PostMapping("/list")
	@RequiresPermissions("system:dict:list")
	@ResponseBody
	public TableDataInfo list(SysDictType dictType) {
		startPage();
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		return getDataTable(list);
	}

	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	@ApiOperation(value = "字典列表", notes = "导出数据字典列表信息")
	@RequiresPermissions("system:dict:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
		return util.exportExcel(list, "dictType");
	}

	/**
	 * 新增字典类型
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增类型", notes = "新增数据字典类型信息")
	@RequiresPermissions("system:dict:add")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysDictType dict) {
		dict.setCreateUser(ShiroUtils.getLoginName());
		return toResult(dictTypeService.insertDictType(dict));
	}

	/**
	 * 修改字典类型
	 */
	@GetMapping("/edit/{dictId}")
	public String edit(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
		modelMap.put("dict", dictTypeService.selectDictTypeById(dictId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存字典类型
	 */
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改类型", notes = "修改数据字典类型信息")
	@RequiresPermissions("system:dict:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysDictType dict) {
		dict.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(dictTypeService.updateDictType(dict));
	}

	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除类型", notes = "删除数据字典类型信息")
	@RequiresPermissions("system:dict:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			return toResult(dictTypeService.deleteDictTypeByIds(ids));
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 查询字典详细
	 */
	@RequiresPermissions("system:dict:list")
	@GetMapping("/detail/{dictId}")
	public String detail(@PathVariable("dictId") Long dictId, ModelMap modelMap) {
		modelMap.put("dict", dictTypeService.selectDictTypeById(dictId));
		modelMap.put("dictList", dictTypeService.selectDictTypeAll());
		return "system/dict/data/data";
	}

	/**
	 * 校验字典类型
	 */
	@PostMapping("/checkDictTypeUnique")
	@ApiOperation(value = "校验类型", notes = "校验数据字典类型唯一")
	@ResponseBody
	public String checkDictTypeUnique(SysDictType dictType) {
		return dictTypeService.checkDictTypeUnique(dictType);
	}
}
